package com.qztech.sdk.ui.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.qztech.bluetooth.BluetoothClientManager;
import com.qztech.sdk.R;
import com.qztech.sdk.ui.activities.BaseFragmentActivity;
import com.qztech.utils.YzDebug;

import static com.qztech.sdk.ui.activities.BaseFragmentActivity.SCREEN_ORIENTATION_LANDSCAPE;

/**
 * Copyright (C), 深圳市东乘电子科技有限公司
 * Carate By YangZhong
 */
public abstract class BaseHeadTailFragment extends BaseFragment {

    private ImageButton mIvSettings;
    private boolean isLandscape = false;
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initHead();
        initTail();
    }

    @Override
    public void updateByConnected(boolean isConnected) {
        updateTopSettingsBackground();
    }

    private void updateTopSettingsBackground(){
        if (mIvSettings != null ){
            if (BluetoothClientManager.getInstance(getActivity()).isConnect()){
                mIvSettings.setImageResource(isLandscape?
                        com.hanlink.res.R.drawable.top_settings_land_conn:com.hanlink.res.R.drawable.top_settings_conn);
            }else {
                mIvSettings.setImageResource(isLandscape?
                        com.hanlink.res.R.drawable.top_settings_land:com.hanlink.res.R.drawable.top_settings);
            }
        }
    }
    private void initHead(){
        View tmp = findViewById(R.id.btn_home);
        if (tmp != null)
            tmp.setOnClickListener(v->getActivity().finish());

        final Activity activity = getActivity();
        if (activity != null && activity instanceof BaseFragmentActivity){
            final int currentBottomBtnId = getCurrentBottomBtnId();
            isLandscape = currentBottomBtnId == R.id.bottom_btn_summing
                    || currentBottomBtnId == R.id.bottom_btn_eq;
            final BaseFragmentActivity baseActivity = (BaseFragmentActivity)activity;
            mIvSettings = findViewById(R.id.btn_settings);
            if (mIvSettings != null){
                updateTopSettingsBackground();
                mIvSettings.setOnClickListener(v->baseActivity.showDeviceDialog(isLandscape));
            }
            tmp = findViewById(R.id.btn_save);
            if (tmp != null){
                tmp.setOnClickListener(v->baseActivity.showSaveDialog(isLandscape));
            }
            tmp = findViewById(R.id.btn_load);
            if (tmp != null){
                tmp.setOnClickListener(v->baseActivity.readData());
            }
            tmp = findViewById(R.id.bottom_logo);
            if (tmp != null){
                tmp.setOnClickListener(v->baseActivity.finish());
            }
        }
        YzDebug.d("initHead:" + activity.getClass().getName());

    }
    private void initTail(){
        final int ids[] = new int[]{
                R.id.bottom_btn_summing,
                R.id.bottom_btn_xover,
                R.id.bottom_btn_delay,
                R.id.bottom_btn_eq,
        };
        final View.OnClickListener l = v->{
            if (mBottonBtnClickListener != null){
                mBottonBtnClickListener.onClick(v);
            }
        };
        final int currentBottomBtnId = getCurrentBottomBtnId();
        View view ;
        int t = 0;

        for (int id : ids) {
            t++;
            if (id == currentBottomBtnId)
                continue;
            view = findViewById(id);
            if (view != null)
                view.setOnClickListener(l);

        }
        view = findViewById(currentBottomBtnId);
        if (view != null && view instanceof ImageButton){
            // can't use switch, because the ID in the module
            if (currentBottomBtnId == R.id.bottom_btn_summing){
                ((ImageButton)view).setImageResource(com.hanlink.res.R.drawable.tab_summing_pressed);
            }else if (currentBottomBtnId == R.id.bottom_btn_xover){
                ((ImageButton)view).setImageResource(com.hanlink.res.R.drawable.tab_xover_pressed);
            }else if (currentBottomBtnId == R.id.bottom_btn_delay){
                ((ImageButton)view).setImageResource(com.hanlink.res.R.drawable.tab_time_delay_pressed);
            }else if (currentBottomBtnId == R.id.bottom_btn_eq){
                ((ImageButton)view).setImageResource(com.hanlink.res.R.drawable.tab_eq_pressed);
            }
        }

    }


    protected abstract @IdRes int  getCurrentBottomBtnId();
    public void setTitle(String title) {
//        Log.d("TT","setTitle:"+ mRootView, new Throwable());
        final TextView titleView =  findViewById(R.id.base_layout_head_title);
        if (titleView != null){
            titleView.setText(title);
        }
    }

    private View.OnClickListener mBottonBtnClickListener;
    public void setOnBottomBtnClickListener(View.OnClickListener bottomBtnClickListener){
        mBottonBtnClickListener = bottomBtnClickListener;
    }
}
